package top.lcm668.study.oj.base.sortmethods;

/**
 * @author: Coder-Lcm
 * @date: 2021/10/5 6:01
 * 选择排序算法 实现
 */
public class SelectSort<T extends Comparable<T>> extends BaseSort<T> {

    @Override
    protected void sort(T[] arr, boolean isAsc) {
        int len = arr.length;
        boolean sorted = false;
        for (int i = 0; i < len; i++) {
            int swapIndex = i;
            sorted = true;
            for (int i1 = i + 1; i1 < len; i1++) {
                if (isAsc ? lessThan(arr[i1], arr[swapIndex]) : largeThan(arr[i1], arr[swapIndex])) {
                    swapIndex = i1;
                    sorted = false;
                }
            }
            // 必要的时候才需要交换
            if (!sorted) {
                swap(arr, i, swapIndex);
            }
        }
    }
}
